//
//  TableViewCell.m
//  CustomTbCellOptimizationObjC
//
//  Created by liy on 2024/10/9.
//

#import "TableViewCell.h"

@interface TableViewCell ()

@property (weak, nonatomic) UIImageView *headImageView;
@property (weak, nonatomic) UILabel *nicknameLabel;
@property (weak, nonatomic) UIImageView *vipImageView;
@property (weak, nonatomic) UILabel *contentLabel;
@property (weak, nonatomic) UIImageView *pictureImageView;

@end

@implementation TableViewCell


	// 1. 初始化子视图
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
			// 头像
		UIImageView *headImageView = [[UIImageView alloc] init];
		[self.contentView addSubview:headImageView];
		self.headImageView = headImageView;
		
			// 昵称
		UILabel *nicknameLabel = [[UILabel alloc] init];
		nicknameLabel.font = [UIFont systemFontOfSize:17.f];
		[self.contentView addSubview:nicknameLabel];
		self.nicknameLabel = nicknameLabel;
		
			// 图标
		UIImageView *vipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip"]];
		[self.contentView addSubview:vipImageView];
		self.vipImageView = vipImageView;
		
			// 正文
		UILabel *contentLabel = [[UILabel alloc] init];
		contentLabel.numberOfLines = 0;
		contentLabel.font = [UIFont systemFontOfSize:14.f];
		[self.contentView addSubview:contentLabel];
		self.contentLabel = contentLabel;
		
			// 配图
		UIImageView *pictureImageView = [[UIImageView alloc] init];
		[self.contentView addSubview:pictureImageView];
		self.pictureImageView = pictureImageView;
	}
	
	return self;
}


	// 2. 布局子视图
- (void)layoutSubviews {
	[super layoutSubviews];
	
	self.headImageView.frame = self.status.cellFrame.headFrame;
	self.nicknameLabel.frame = self.status.cellFrame.nicknameFrame;
	self.vipImageView.frame = self.status.cellFrame.vipFrame;
	self.contentLabel.frame = self.status.cellFrame.contentFrame;
	self.pictureImageView.frame = self.status.cellFrame.pictureFrame;
}


	// 3. 填充数据
- (void)setStatus:(Status *)status {
	_status = status;
	
		// head
	self.headImageView.image = [UIImage imageNamed:status.icon];
	
		// nickname & vip
	self.nicknameLabel.text = status.name;
	if (status.isVip) {
		self.nicknameLabel.textColor = [UIColor orangeColor];
		self.vipImageView.hidden = NO;
	} else {
		self.nicknameLabel.textColor = [UIColor blackColor];
		self.vipImageView.hidden = YES;
	}
	
		// content
	self.contentLabel.text = status.text;
	
		// picture
	if (status.picture != nil) {
		self.pictureImageView.hidden = NO;
		self.pictureImageView.image = [UIImage imageNamed:status.picture];
	} else {
		self.pictureImageView.hidden = YES;
	}
}

@end
